from locust import HttpUser,task,between
import requests, json
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad

def encrypt(plain_text, key):
    cipher = AES.new(key, AES.MODE_ECB)
    padded_text = pad(plain_text.encode(), AES.block_size)
    encrypted_text = cipher.encrypt(padded_text)
    return encrypted_text.hex()

def decrypt(encrypted_text, key):
    cipher = AES.new(key, AES.MODE_ECB)
    decrypted_text = cipher.decrypt(bytes.fromhex(encrypted_text))
    return unpad(decrypted_text, AES.block_size).decode()


key = bytes('1111111111111111',encoding="utf-8")

class MyUser(HttpUser):
    wait_time = between(1,2)

    @task
    def index_page(self):
        payload = {
            "data": "8B6D040----------------------------****************4CD8CB1359C8"
        }
        res=self.client.post('https://************************/loginByPhoneCode', json=payload)
        decrypted_text = decrypt(json.loads(res.content.decode('utf8'))['data'], key)
        print("解密后的结果:", json.loads(decrypted_text))
        token = json.loads(decrypted_text)['extInfo']['x-token']
        print(token)

        plain_text = '''{
          "pageNo": 1,
          "pageSize": 15
        }'''
        payload2 = {
            "data": encrypt(plain_text, key)
        }
        header = {
            'Content-Type': 'application/json;charset=UTF-8',
            'X-Token': token
        }
        self.client.post('https://**************************/listMasterByPage',headers=header,json=payload2)